﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Windows;

namespace WindowPlace
{
    public class WindowPlaceSettings
    {
        private DateTime LastSession_ = DateTime.Now;
        public DateTime LastSession
        {
            get { return LastSession_; }
            set { LastSession_ = value; }
        }

        private List<PlaceProfile> PlaceProfiles_ = new List<PlaceProfile>();
        public List<PlaceProfile> PlaceProfiles
        {
            get
            {
                return PlaceProfiles_;
            }
            set
            {
                PlaceProfiles_ = value;
            }
        }

        public Version Version
        {
            get
            {
                return Version_;
            }
            set
            {
                Version_ = value;
            }
        }
        private Version Version_;

        public bool ConsumeKeyboardEvents
        {
            get
            {
                return ConsumeKeyboardEvents_;
            }
            set
            {
                ConsumeKeyboardEvents_ = value;
            }
        }
        private bool ConsumeKeyboardEvents_ = true;

        public bool PlusControl
        {
            get
            {
                return PlusControl_;
            }
            set
            {
                PlusControl_ = value;
                OnHotKeyCombinationChanged();
            }
        }
        private bool PlusControl_ = true;

        public bool PlusShift
        {
            get
            {
                return PlusShift_;
            }
            set
            {
                PlusShift_ = value;
                OnHotKeyCombinationChanged();
            }
        }
        private bool PlusShift_ = false;

        public bool PlusAlt
        {
            get
            {
                return PlusAlt_;
            }
            set
            {
                PlusAlt_ = value;
                OnHotKeyCombinationChanged();
            }
        }
        private bool PlusAlt_ = false;

        public int StartupWaitTime
        {
            get
            {
                return StartupWaitTime_;
            }
            set
            {
                StartupWaitTime_ = value;
            }
        }
        private int StartupWaitTime_ = 1500;

        private void OnHotKeyCombinationChanged()
        {
            if (HotKeyCombinationChanged != null)
            {
                HotKeyCombinationChanged(this, null);
            }
        }

        public event EventHandler HotKeyCombinationChanged;

        #region Load and Save

        public static WindowPlaceSettings Load(string FullFilename)
        {
            Debug.Print("WindowPlaceSettings::Load FullFilename = {0}", FullFilename);
            if (File.Exists(FullFilename))
            {
                Debug.Print("   {0} Exists", FullFilename);
                try
                {
                    WindowPlaceSettings settings;
                    XmlReader reader = XmlReader.Create(FullFilename);
                    XmlSerializer x = new XmlSerializer(typeof(WindowPlaceSettings));

                    settings = (WindowPlaceSettings)x.Deserialize(reader);
                    Debug.Print("   WindowPlaceSettings loaded ok");
                    reader.Close();

                    //wipe Caption from WindowInfos
                    int counter = 0;
                    foreach (PlaceProfile pp in settings.PlaceProfiles)
                    {
                        pp.WindowInfos.ForEach(a => a.Caption = string.Empty);
                        pp.Index = counter;
                        counter++;
                    }

                    return settings;
                }
                catch (Exception)
                {

                    MessageBox.Show(string.Format("Error loading the Settings file {0}", FullFilename));
                    return new WindowPlaceSettings();
                }
            }
            else
            {
                Debug.Print("   {0} does not Exist", FullFilename);
                return new WindowPlaceSettings();
            }
        }

        public static void Save(WindowPlaceSettings Settings, string FullFilename)
        {
            Debug.Print("WindowPlaceSettings::Save FullFilename = {0}", FullFilename);
            try
            {
                XmlSerializer x = new XmlSerializer(typeof(WindowPlaceSettings));
                XmlWriter writer = XmlWriter.Create(FullFilename);
                x.Serialize(writer, Settings);
                writer.Close();
            }
            catch (Exception)
            {
                MessageBox.Show(string.Format("Error saving the Settings file '{0}", FullFilename));

            }

        }

        #endregion
    }
}
